import time

import pandas as pd
import pyperclip
from pywinauto.application import Application
from pywinauto.keyboard import send_keys

from tools.复制文件到剪切板 import setClipboardFile


def wechat_send_file(user_name, file_path):
    Application(backend='uia').start(r"D:\Program Files (x86)\WeChat\WeChat.exe")
    app = Application(backend='uia').connect(path=r"D:\Program Files (x86)\WeChat\WeChat.exe")
    main_window = app.window(title='微信')
    edit_搜索 = main_window.child_window(title='搜索')
    edit_搜索.click_input()
    send_keys('^a', pause=0)
    send_keys('{DELETE}', pause=0)
    pyperclip.copy(user_name)
    send_keys('^v')
    time.sleep(1)
    搜索的群聊 = main_window.child_window(title_re='<em>' + user_name + '.*', control_type='Text')
    搜索的群聊.click_input()
    edit_msg = main_window.child_window(title_re=user_name, control_type='Edit')
    edit_msg.click_input()
    setClipboardFile(file_path)
    send_keys('^v')
    send_keys('{ENTER}')
    time.sleep(1)


def wechat_send_msg(user_name, file_path):
    Application(backend='uia').start(r"D:\Program Files (x86)\WeChat\WeChat.exe")
    app = Application(backend='uia').connect(path=r"D:\Program Files (x86)\WeChat\WeChat.exe")
    main_window = app.window(title='微信')
    edit_搜索 = main_window.child_window(title='搜索')
    edit_搜索.click_input()
    send_keys('^a', pause=0)
    send_keys('{DELETE}', pause=0)
    pyperclip.copy(user_name)
    send_keys('^v')
    time.sleep(1)
    搜索的群聊 = main_window.child_window(title_re='<em>' + user_name + '.*', control_type='Text')
    搜索的群聊.click_input()
    edit_msg = main_window.child_window(title_re=user_name, control_type='Edit')
    edit_msg.click_input()
    df = pd.read_excel(file_path)
    if '发送状态' not in df.columns:
        df['发送状态'] = ''
    for index, row in df.iterrows():
        if row['发送状态'] == '已发送':
            print(f"已发送")
            continue
        msg = str(row.iloc[1]) + '\n' + row.iloc[4] + '\n' + '物流长时间中断，找到退回，找不到理赔'
        pyperclip.copy(msg)
        send_keys('^v')
        send_keys('{ENTER}')
        time.sleep(0.5)
        setClipboardFile(row.iloc[2])
        send_keys('^v')
        send_keys('{ENTER}')
        time.sleep(0.5)
        df.at[index, '发送状态'] = '已发送'
        df.to_excel(file_path, index=False)


def QQ_send_file(user_name, file_path):
    app = Application(backend='uia').connect(title_re="任务栏")
    main_window = app.window(title_re='任务栏')
    # b = main_window.child_window(title_re='1.*QQ.*', control_type='Button').child_window(class_name='Image')
    b = main_window.child_window(title_re='QQ.*', control_type='Button').child_window(class_name='Image')
    b.click_input()
    app = Application(backend='uia').connect(title_re="QQ")
    main_window = app.window(title='QQ')
    edit_搜索 = main_window.child_window(title='搜索', control_type='ComboBox')
    edit_搜索.click_input()
    send_keys('^a', pause=0)
    send_keys('{DELETE}', pause=0)
    pyperclip.copy(user_name)
    send_keys('^v')
    time.sleep(1)
    搜索的群聊 = main_window.child_window(title=user_name, control_type='ListItem')
    搜索的群聊.click_input()
    edit_msg = main_window.child_window(title_re='.*' + user_name + '.*', control_type='Edit')
    edit_msg.click_input()
    setClipboardFile(file_path)
    send_keys('^v')
    edit_msg = main_window.child_window(title='发送(1)', control_type='Button')
    edit_msg.click_input()
    time.sleep(1)


def QQ_send_msg(user_name, file_path):
    app = Application(backend='uia').connect(title_re="任务栏")
    main_window = app.window(title_re='任务栏')
    # QQ = main_window.child_window(title_re='1.*QQ.*', control_type='Button').child_window(class_name='Image')
    QQ = main_window.child_window(title_re='QQ.*', control_type='Button').child_window(class_name='Image')
    QQ.click_input()
    app = Application(backend='uia').connect(title_re="QQ")
    main_window = app.window(title='QQ')
    edit_搜索 = main_window.child_window(title='搜索', control_type='ComboBox')
    edit_搜索.click_input()
    send_keys('^a', pause=0)
    send_keys('{DELETE}', pause=0)
    pyperclip.copy(user_name)
    send_keys('^v')
    time.sleep(1)
    搜索的群聊 = main_window.child_window(title=user_name, control_type='ListItem')
    搜索的群聊.click_input()
    edit_msg = main_window.child_window(title_re='.*' + user_name + '.*', control_type='Edit')
    edit_msg.click_input()
    df = pd.read_excel(file_path)
    if '发送状态' not in df.columns:
        df['发送状态'] = ''
    for index, row in df.iterrows():
        if row['发送状态'] == '已发送':
            print(f"已发送")
            continue
        msg = str(row.iloc[1]) + '\n' + row.iloc[4] + '\n' + '物流长时间中断，找到退回，找不到理赔'
        pyperclip.copy(msg)
        send_keys('^v')
        send_keys('{ENTER}')
        time.sleep(0.5)
        setClipboardFile(row.iloc[2])
        send_keys('^v')
        send_keys('{ENTER}')
        time.sleep(0.5)
        df.at[index, '发送状态'] = '已发送'
        df.to_excel(file_path, index=False)
